﻿using System;
using System.Windows.Automation.Peers;
using System.Windows.Controls;
using Panuon.UI.Silver;

namespace FESSC_WORKBENCH
{
    /// <summary>
    /// WeekReport.xaml 的交互逻辑
    /// </summary>
    public partial class WeekReport : Page
    {
        private ScrollViewer treeViewScroller;

        public WeekReport()
        {
            InitializeComponent();
            listInit();
            tabInit();
        }

        private void listInit()
        {
            int year = DateTime.Now.Year;
            for (int i = year; i >= 2012; i--)
            {
                TreeViewItem item = new TreeViewItem
                {
                    Tag = i,
                    Header = i.ToString() + "年"
                };
                createWeekList(item);
                TV_WEEKLIST.Items.Add(item);
            }
        }

        private void createWeekList(TreeViewItem item)
        {
            int year = (int)(item.Tag);
            int maxweek = DateFunc.GetMaxWeek(year);
            DateTime dt = new DateTime(year, 1, 1);
            int adddays = DateFunc.GetAddDays(year);
            dt = dt.AddDays((-1) * adddays);
            string text = string.Empty;
            TreeViewItem weekitem(string header, int week)
            {
                TreeViewItem wi = new TreeViewItem
                {
                    Header = text,
                    Tag = week,
                    FontSize = 10
                };
                return wi;
            }
            for (int i = 1; i <= maxweek; i++)
            {
                text = string.Format(" 第{0}周 - {1}～", i, dt.ToString("M月d日"));
                dt = dt.AddDays(6);
                text += dt.ToString("M月d日");
                item.Items.Add(weekitem(text, i));
                dt = dt.AddDays(1);
            }
            if (Convert.ToInt32(new DateTime(year, 12, 31).DayOfWeek) != 0)
                item.Items.Add(weekitem(" 跨年周 - 本周视为次年第一周", 99));
        }

        private void tabInit()
        {
            TabItem def = new TabItem();
            def.Header = AppVar.userinfo.deptname;
            def.Tag = AppVar.userinfo.deptid;
            TC_DEPT.Items.Add(def);
        }

        private void Page_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            TreeViewAutomationPeer tvap = new TreeViewAutomationPeer(TV_WEEKLIST);
            var svap = tvap.GetPattern(PatternInterface.Scroll) as ScrollViewerAutomationPeer;
            treeViewScroller = svap.Owner as ScrollViewer;
            treeViewScroller.ScrollToTop();
            var week = DateFunc.GetWeek(DateTime.Now);
            (TV_WEEKLIST.Items[0] as TreeViewItem).ExpandSubtree();
            var item = (TV_WEEKLIST.Items[0] as TreeViewItem).Items[week - 1] as TreeViewItem;
            item.IsSelected = true;
            item.BringIntoView();
        }
    }
}
